--
-- Author: shunguo.chen
-- Date: 2016-11-15 18:16:48
--
-- PayPanel 充值

local PayRender = import(".renders.PayRender")
local ResRender = require("app.widget.renders.ResRender")

local ClassRef = app.mvc.createPanelClass(...)

function ClassRef.create(node)
	local sprite = ClassRef.extend(node)
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self._scrollView = self._ui["Panel/ScrollView"]
	self._resRender = ResRender.create(self._ui.Panel_money)
	self._resRender:setBtnVisible(false)
	self._resRender:setData({type = Enums.TipsType.daibi, id = Enums.PropType.coin, num = 0})
	
	self._allRenders = {}
	self._cacheRenders = {}

	-- local panel = self._ui.Panel_mid
	-- local renders = {0,0,0,0,0}
	-- for i=1,5 do
	-- 	local render = PayRender.create(panel:getChildByName("node_" .. i))
	-- 	render:onBtnClick(function(data)
	-- 		executeFunc(self._onOperate, "pay", data)
	-- 	end)
	-- 	renders[i] = render
	-- end
	-- self._renders = renders

	self:reloadData(data)
end

function ClassRef:onOperate(cb)
	self._onOperate = cb
end

function ClassRef:updateCoin(num)
	self._resRender:setNum(num)
end

function ClassRef:getRenders()
	return self._renders
end


function ClassRef:refreshDataByPayData(payData)
	for i,render in ipairs(self._allRenders) do
		local data = render:getData()
		if data and data.id == payData.id then
			render:refresh()
			break
		end
	end
end

function ClassRef:reloadData(data)
	if data == nil then
		return
	end
	local num = #data
	local renderNum = #self._allRenders
	
	
	if renderNum < num then
		for i = 1, num - renderNum do
			local render = PayRender.create()
			render:onBtnClick(function(data)
				executeFunc(self._onOperate, "pay", data)
			end)
			-- render:setVisible(false)
			self._scrollView:addChild(render)
			table.insert(self._allRenders, render)
		end
	end

	local renderWidth = 0
	local width = 0
	for k, v in ipairs(self._allRenders) do
		if renderWidth == 0 then
			renderWidth = v:getRenderWidth()
		end
		if k <= num then
			v:setVisible(true)
			v:setData(data[k])
			v:setPositionX(width)
			width = width + renderWidth + 5
		else
			v:setVisible(false)
		end
	end
	local height = self._scrollView:getContentSize().height
	self._scrollView:setInnerContainerSize(cc.size(width, height))

	-- 数目变化
	if num ~= renderNum then
		self._scrollView:jumpToLeft()
	end
end

return ClassRef